package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.mapper.AdChannelMapper;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.service.AdChannelService;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdchannelServiceimpl extends BaseServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
//1.参数检测
        if(dto==null)return paramError;

        //2.安装名称模糊分页查询
        Page page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }
//        IPage result = page(page, lambdaQueryWrapper);
//
//        //3.结果封装
//        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)result.getTotal());
//        responseResult.setData(result.getRecords());
        return selectFindAll(dto,page,lambdaQueryWrapper);
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        //检查参数
        if (null==channel)return paramError;
        channel.setCreatedTime(new Date());
        //保存
        save(channel);
        return okResult;
    }

    @Override
    public ResponseResult update(AdChannel channel) {
        if (null==channel || channel.getId()==null)return  paramError;

        updateById(channel);
        return okResult;
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if (id==null)return paramError;
        AdChannel byId = getById(id);
        if(byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(byId.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效不能删除");
        }

        removeById(id);
        return okResult;
    }
}
